package ${packageName}.${moduleName}.web;

<#assign ClassName=StringUtil.firstToUpper(className)>
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cnlot.booking.base.JsonResponse;
import com.cnlot.booking.base.Page;
import com.cnlot.booking.base.Parameter;
import com.cnlot.booking.manager.util.RequestUtil;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ${packageName}.domain.${ClassName};
import ${packageName}.manager.service.${ClassName}Service;

@Controller
@RequestMapping(value = "/${className}")
public class ${ClassName}Controller {
	private static final Logger log = LoggerFactory.getLogger(${ClassName}Controller.class);

	@Autowired ${ClassName}Service ${className}Service;

	@ResponseBody
	@RequestMapping(value = "/create",produces="application/json;charset=UTF-8")
	public String create(HttpServletRequest request,${ClassName} entity){
		try{
			${className}Service.create(entity);
			return JsonResponse.OK;
		}catch(Exception e){
			RequestUtil.debugParam(request);
			log.error("create ${className} error",e);
			return JsonResponse.FAIL;
		}
	}

	@ResponseBody
	@RequestMapping(value = "/get")
	public JsonResponse get(@RequestParam(required=false) Long id){
		JsonResponse response=new JsonResponse();
		${ClassName} obj=${className}Service.get(id);
		if(obj==null){
			return response.fail().message("获取失败");
		}else{
			return response.ok().entity(obj);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/update",produces="application/json;charset=UTF-8")
	public String update(HttpServletRequest request){
		Parameter parameter=RequestUtil.toParameter(request);
		try{
			${className}Service.update(parameter);
			return JsonResponse.OK;
		}catch(Exception e){
			RequestUtil.debugParam(request);
			log.error("update ${ClassName} error",e);
			return JsonResponse.FAIL;
		}
	}

	@ResponseBody
	@RequestMapping(value = "/delete",produces="application/json;charset=UTF-8")
	public String delete(HttpServletRequest request){
		Parameter parameter=RequestUtil.toParameter(request);
		try{
			${className}Service.update(parameter);
			return JsonResponse.OK;
		}catch(Exception e){
			RequestUtil.debugParam(request);
			log.error("delete ${ClassName} error",e);
			return JsonResponse.FAIL;
		}
	}



	/**
	 *
	 {"code": 200,"data": {"message": "success","total": 100,"pageNo": 2,"pageSize": 20,"list": [{"id": 1,"name": "娜姐"}]}}
	 */
	@ResponseBody @RequestMapping(value = "/list")
	public JsonResponse list(HttpServletRequest request){
		JsonResponse response=new JsonResponse();
		Page<${ClassName}> page=new Page<${ClassName}>();
		page.setTotal(RequestUtil.get(request, "total", 0L));
		page.setPageNo(RequestUtil.get(request, "pageNo", 0L));
		//page.setPageSize(); default 20

		page.add("locked", RequestUtil.get(request, "status", 0));
		page.add("username", RequestUtil.get(request,"name",null));

		response.ok().setData(${className}Service.list(page));

		page.getFilter().clear();

		return response;
	}
}
